package price

import (
	"time"
)

type TimePrice struct {
	StartTime string
	StopTime  string
	Price     float64
}

type ChargeEvent struct {
	StartTime time.Time
	EndTime   time.Time
	Action    string // "charge" or "discharge"
	Price     float64
}

func CalculateChargeEvents(timePrices []TimePrice, date time.Time) []ChargeEvent {
	var events []ChargeEvent
	loc := date.Location()

	// 计算平均价格作为参考
	var sum float64
	for _, tp := range timePrices {
		sum += tp.Price
	}
	avgPrice := sum / float64(len(timePrices))

	for _, tp := range timePrices {
		start, _ := time.ParseInLocation("15:04:05", tp.StartTime, loc)
		end, _ := time.ParseInLocation("15:04:05", tp.StopTime, loc)

		start = time.Date(date.Year(), date.Month(), date.Day(), start.Hour(), start.Minute(), start.Second(), 0, loc)
		end = time.Date(date.Year(), date.Month(), date.Day(), end.Hour(), end.Minute(), end.Second(), 0, loc)

		action := "charge"
		if tp.Price > avgPrice { // 当价格高于平均价格时放电
			action = "discharge"
		}

		events = append(events, ChargeEvent{
			StartTime: start,
			EndTime:   end,
			Action:    action,
			Price:     tp.Price,
		})
	}

	return events
}
